HAOJX

kubernetes HPA

字数统计: 247阅读时长: 1 min
2019/09/02 Share

查看帮助

1
kubectl explain hpa

先创建一个有资源限制的pod

1
kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=1 --requests='cpu=50m,memory=256Mi' --limits='cpu=50m,memory=256Mi' --labels='app=myapp' --expose --port=80

创建一个HPA

1
kubectl autoscale --help  //查看帮助
1
kubectl autoscale deployment myapp --min=1 --max=8 --cpu-percent=60
1
kubectl patch svc myapp -p '{"spec":{"type":"NodePort"}}'
1
kubectl get svc  //查看这个服务端口
1
2
yum install -y httpd-tools
ab -c 1000 -n 500000 http://IP:PORT/index.html

查看效果

1
2
kubectl  get pods
kubectl describe hpa

vim myapp-hpa-demo.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: mapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 1
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 55
- type: Resource
resource:
name: memory
targetAverageValue: 50Mi

应用这些文件

1
kubectl apply -f myapp-hpa-demo.yaml

压测

1
2
yum install -y httpd-tools
ab -c 1000 -n 500000 http://IP:PORT/index.html

查看效果

1
2
kubectl  get pods
kubectl describe hpa

可以看到会自动扩展

比如其他的指标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: mapp-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: myapp
minReplicas: 1
maxReplicas: 10
metrics:
- type: Pods
pods:
metricName: http_requests
targetAverageValue: 800m
CATALOG